# -*- coding: utf_8 -*-
# @Create   : 2021/8/30 10:17
# @Author   : yh
# @Remark   : 巡检计划view层

from mxsoftpy import View

from server.common.VideoManage.VideoQuality.inspection_plan import InspectionPlanServer
from utils.public.params_method import check_params, pro_decode


def _check_videos(videos):
    # 校验videos格式是否正确
    # noinspection PyBroadException
    try:
        videos = eval(videos)
        if isinstance(videos, dict):
            check_list = list(videos.keys())
            check_params(videos, ['group', 'video'])
            return check_list
        else:
            return False
    except Exception:
        return False


def _check_items(items):
    # 校验test_items格式是否正确
    # noinspection PyBroadException
    try:
        test_items = eval(items)
        if isinstance(test_items, list):
            for item in test_items:
                check_params(item, ['test_code', 'confidence'])
            return test_items
        else:
            return False
    except Exception:
        return False


class InspectionPlanAdd(View):

    def post(self):
        """
        添加巡检计划
        """
        check_params(self.request.POST,
                     ['name', 'principal', 'task_cycle_code', 'begin_time', 'videos', 'TestItems'])

        self.request.POST['TestItems'] = _check_items(self.request.POST['TestItems'])
        param = pro_decode(**self.request.POST)
        for i in param['videos'].split(';'):
            if not i.isdigit():
                return '错误的videos数据，有数据无法转为int类型'
        if param['TestItems'] is False:
            return 'test_items属性错误，无法序列化或不是List'
        else:
            return '添加巡检计划成功', {'plan_code': InspectionPlanServer().create(**param)}


class InspectionPlanDelete(View):

    def post(self):
        """
        删除巡检计划
        """
        check_params(self.request.POST, ['plan_code'])
        return '删除巡检计划成功', InspectionPlanServer().delete(self.request.POST['plan_code'])


class InspectionPlanStop(View):

    def post(self):
        """
        停用巡检计划
        """
        check_params(self.request.POST, ['plan_code'])
        return '停用巡检计划成功', InspectionPlanServer().stop(self.request.POST['plan_code'])


class InspectionPlanStart(View):

    def post(self):
        check_params(self.request.POST, ['plan_code'])
        return '启用巡检计划成功', InspectionPlanServer().start(self.request.POST['plan_code'])


class InspectionPlanStartCheck(View):

    def post(self):
        check_params(self.request.POST, ['plan_code'])
        return '启用巡检计划校验成功', {'data': InspectionPlanServer().start_check(self.request.POST['plan_code'])}


class InspectionPlanRetrieve(View):

    def post(self):
        """
        获取巡检计划详情
        """
        check_params(self.request.POST, ['plan_code'])
        return '获取巡检计划详情成功', InspectionPlanServer().retrieve(self.request.POST['plan_code'])


class InspectionPlanUpdate(View):

    def post(self):
        """
        更新巡检计划
        """
        check_params(self.request.POST,
                     ['name', 'principal', 'task_cycle_code', 'begin_time', 'videos', 'TestItems', 'plan_code'])

        for i in self.request.POST['videos'].split(';'):
            if not i.isdigit():
                return '错误的videos数据，有数据无法转为int类型'

        self.request.POST['TestItems'] = _check_items(self.request.POST['TestItems'])
        param = pro_decode(**self.request.POST)
        if param['TestItems'] is False:
            return 'test_items属性错误，无法序列化或不是List'
        else:
            return '更新巡检计划成功', {'plan_code': InspectionPlanServer().update(**param)}


class InspectionPlanSelect(View):

    def post(self):
        """
        模糊查询巡检计划
        """
        check_params(self.request.POST, ['name'])
        return '查询巡检计划成功', InspectionPlanServer().select(**self.request.POST)
